exclude_set = c(
  colnames(dta_grp)[str_detect(colnames(dta_grp), "demog_")],
  colnames(dta_grp)[str_detect(colnames(dta_grp), "policy_")],
  colnames(dta_grp)[str_detect(colnames(dta_grp), "_pres")],
  colnames(dta_grp)[str_detect(colnames(dta_grp), "_local")],
  colnames(dta_grp)[str_detect(colnames(dta_grp), "_grp")]
)

which_effects_dta_grp =
  setdiff(
    colnames(dta_grp),
    # Which columns actually have dta_grp data to fill in?
    c("choice", "person_id", "question", "set","ResponseId",
      "user_agent","user_agent_type","user_agent_ver","user_agent_os","user_agent_os_ver",
      "week","group","wave","race_pres","race_local", exclude_set))

dta_grp_int = read_parquet(here("data","dta_grp_int.parquet"), as_data_frame = T)

# Code agreement with the positions in the dta_grp
for(i in which_effects_dta_grp){
  dta_grp_int[[paste("agreement_",i,sep="")]] <-
    ifelse(dta_grp_int[[i]]==dta_grp_int[[paste("policy_",i,sep="")]],1,0)
  dta_grp_int[[paste("agreement_",i,sep="")]][dta_grp_int[[i]] %in% c(3,999)] <- NA
  dta_grp_int[[paste("agreement_",i,sep="")]][is.na(dta_grp_int[[i]])] <- NA
}
positions_agreement <- paste("agreement_",which_effects_dta_grp,sep="")

econ = c("gov_insurance", "public_option", "health_subsidies",
         "mctaxes", "estate_tax", "uctaxes", "college",
         "trade", "minwage", "gauranteed_jobs", "right_to_work",
         "medicare_for_all", "uctaxes2")

agree_econ = paste0("agreement_", econ)
agree_nonecon = setdiff(positions_agreement, agree_econ)

dta_agree_sub = dta_grp_int |> 
  # For each profile, do you agree/disagree with an econ/nonecon position?
  mutate(agree_econ = if_any(all_of(agree_econ), ~ . == 1),
         agree_nonecon = if_any(all_of(agree_nonecon), ~ . == 1),
         disagree_econ = if_any(all_of(agree_econ), ~ . == 0),
         disagree_nonecon = if_any(all_of(agree_nonecon), ~ . == 0),
         n_econ       = rowSums(across(all_of(agree_econ),    ~ !is.na(.))),
         n_nonecon    = rowSums(across(all_of(agree_nonecon), ~ !is.na(.)))) |>
  replace_na(list(
    agree_econ = FALSE,
    agree_nonecon = FALSE,
    disagree_econ = FALSE,
    disagree_nonecon = FALSE)
  ) |> 
  select(choice, question, set, person_id, ends_with("_econ"), ends_with("_nonecon"),
         ends_with("_grp"), demog_weight) |> 
  group_by(person_id, question) |> 
  # For each profile pair, do you agree/disagree with any econ/nonecon position
  mutate(agree_any_econ = any(agree_econ),
         disagree_any_econ = any(disagree_econ),
         agree_any_nonecon = any(agree_nonecon),
         disagree_any_nonecon = any(disagree_econ)) |> 
  ungroup() |> 
  mutate(agree_both = agree_any_econ & agree_any_nonecon,
         disagree_both = disagree_any_econ & disagree_any_nonecon) |> 
  filter(agree_both | disagree_both) |> 
  filter(n_econ == n_nonecon)

rm(dta_grp_int)

dta_agr = dta_agree_sub |> 
  filter(agree_both)

dta_dgr = dta_agree_sub |> 
  filter(disagree_both)

# Overall
mod_agrecon = felm(choice ~ agree_econ | 0 | 0 | person_id,
                    data = dta_agr,
                    weights = dta_agr$demog_weight )

mod_dgrecon = felm(choice ~ disagree_econ | 0 | 0 | person_id,
                   data = dta_dgr,
                   weights = dta_dgr$demog_weight )

mod_agrnecon = felm(choice ~ agree_nonecon | 0 | 0 | person_id,
                   data = dta_agr,
                   weights = dta_agr$demog_weight )

mod_dgrnecon = felm(choice ~ disagree_nonecon | 0 | 0 | person_id,
                   data = dta_dgr,
                   weights = dta_dgr$demog_weight )

stargazer::stargazer(mod_agrecon, mod_dgrecon, mod_agrnecon, mod_dgrnecon,
                     header = F,
                     type = 'latex',
                     out = here("tables", "appendix", "tab_s10.tex"),
                     covariate.labels = c("Agree (Redistributive)",
                                          "Disagree (Redistributive)",
                                          "Agree (Non-Redistributive)",
                                          "Disagree (Non-Redistributive)"),
                     
                     dep.var.labels = "Selected")

# Income Group
mod_iagrecon = felm(choice ~ agree_econ + agree_econ*inc_grp | 0 | 0 | person_id,
                   data = dta_agr,
                   weights = dta_agr$demog_weight )

mod_idgrecon = felm(choice ~ disagree_econ + disagree_econ*inc_grp| 0 | 0 | person_id,
                   data = dta_dgr,
                   weights = dta_dgr$demog_weight )

mod_iagrnecon = felm(choice ~ agree_nonecon + agree_nonecon*inc_grp | 0 | 0 | person_id,
                    data = dta_agr,
                    weights = dta_agr$demog_weight )

mod_idgrnecon = felm(choice ~ disagree_nonecon + disagree_nonecon*inc_grp | 0 | 0 | person_id,
                    data = dta_dgr,
                    weights = dta_dgr$demog_weight )

stargazer::stargazer(mod_iagrecon, mod_idgrecon, mod_iagrnecon, mod_idgrnecon,
                     header = F, type = 'latex',
                     covariate.labels = c("Agree (Redistributive)",
                                          "Disagree (Redistributive)",
                                          "Agree (Non-Redistributive)",
                                          "Disagree (Non-Redistributive)",
                                          "High Income",
                                          "Agree (R) * High Income",
                                          "Disagree (R) * High Income",
                                          "Agree (NR) * High Income",
                                          "Disagree (NR) * High Income"),
                     dep.var.labels = "Selected",
                     out = here("tables", "appendix", "tab_s11.tex"))

# Education Group
mod_eagrecon = felm(choice ~ agree_econ + agree_econ*edu_grp | 0 | 0 | person_id,
                    data = dta_agr,
                    weights = dta_agr$demog_weight )

mod_edgrecon = felm(choice ~ disagree_econ + disagree_econ*edu_grp| 0 | 0 | person_id,
                    data = dta_dgr,
                    weights = dta_dgr$demog_weight )

mod_eagrnecon = felm(choice ~ agree_nonecon + agree_nonecon*edu_grp | 0 | 0 | person_id,
                     data = dta_agr,
                     weights = dta_agr$demog_weight )

mod_edgrnecon = felm(choice ~ disagree_nonecon + disagree_nonecon*edu_grp | 0 | 0 | person_id,
                     data = dta_dgr,
                     weights = dta_dgr$demog_weight )

stargazer::stargazer(mod_eagrecon, mod_edgrecon, mod_eagrnecon, mod_edgrnecon,
                     header = F, type = 'latex',
                     covariate.labels = c("Agree (Redistributive)",
                                          "Disagree (Redistributive)",
                                          "Agree (Non-Redistributive)",
                                          "Disagree (Non-Redistributive)",
                                          "High Education",
                                          "Agree (R) * High Edu",
                                          "Disagree (R) * High Edu",
                                          "Agree (NR) * High Edu",
                                          "Disagree (NR) * High Edu"),
                     dep.var.labels = "Selected",
                     out = here("tables", "appendix", "tab_s12.tex"))

ccat("See 'tab_s10.tex' to 'tab_s12.tex' for Tables S10-12 (Appendix D)")
